package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private DtsCategoryMapper categoryMapper;

    @Override
    public List<DtsCategory> list() {
        return categoryMapper.selectList(null);
    }

    @Override
    public List<CatVo> list1() {
        List<CatVo> list = categoryMapper.list();
        return list;
    }


    @Override
    public IPage<DtsCategory> queryByPage(Integer id, String name,
                                          Integer page, Integer limit,
                                          String sort, String order) {
        IPage<DtsCategory> iPage = new Page<>(page, limit);

        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        if (!StringUtils.isEmpty(id)) {
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }

        if ("asc".equalsIgnoreCase(order)) {
            queryWrapper.orderByAsc(sort);
        } else {
            queryWrapper.orderByDesc(sort);
        }
        IPage<DtsCategory> categoryIPage = categoryMapper.selectPage(iPage, queryWrapper);
        return categoryIPage;
    }

    @Override
    public DtsCategory addOne(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public int updateOne(DtsCategory category) {
        category.setUpdateTime(new Date());
        int updateById = categoryMapper.updateById(category);
        return updateById;
    }

    @Override
    public void deleteOne(DtsCategory category) {
        categoryMapper.deleteById(category);
    }

    @Override
    public List<DtsCategory> showCat() {
        return categoryMapper.showCategorys();
    }
}